home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / SDKs / Apple Game Sprockets / InputSprocket / Sample⁄Test Applications / InputSprocketPPTest / ISpTestMain.cp < prev    next >
Encoding:
Text File  |  1998-07-17  |  10.5 KB  |  474 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2.  
  3. File:      ISpTestMain.cp
  4.  
  5. Copyright © 1996, 1997, 1998 Apple Computer, Inc., All Rights Reserved
  6.  
  7.  
  8. You may incorporate this sample code into your applications without
  9. restriction, though the sample code has been provided "AS IS" and the
  10. responsibility for its operation is 100% yours.  However, what you are
  11. not permitted to do is to redistribute the source as "DSC Sample Code"
  12. after having made changes. If you're going to re-distribute the source,
  13. we require that you make it clear in the source that the code was
  14. descended from Apple Sample Code, but that you've made changes.
  15.  
  16. *************************************************************************************/
  17.  
  18. #include "ISpTestMain.h"
  19. #include <Types.h>
  20. #include <InputSprocket.h>
  21.  
  22. #include <LGrowZone.h>
  23. #include <LWindow.h>
  24. #include <PP_Messages.h>
  25. #include <PP_Resources.h>
  26. #include <PPobClasses.h>
  27. #include <UDrawingState.h>
  28. #include <UMemoryMgr.h>
  29. #include <URegistrar.h>
  30. #include <LEditField.h>
  31. #include <Events.h>
  32.  
  33. #include "ISpTestGlobals.h"
  34. #include "ISpEventPane.h"
  35. #include "ISpElementView.h"
  36. #include "ISpLQuitWindow.h"
  37.  
  38. // put declarations for resource ids (ResIDTs) here
  39.  
  40. const ResIDT    window_Sample        = 1;    // EXAMPLE
  41.  
  42. const OSType kCreatorCode        = 'ISPP';
  43. const OSType kSubCreatorCode    = '0001';    // we'll use this as versioning for needs list
  44.  
  45. #define    kResourceID_isap                        128
  46. #define    kResourceID_setl                        128
  47.  
  48. Boolean EventProc(EventRecord *inEvent);
  49. Boolean EventProc(EventRecord *inEvent)
  50. {
  51.     if (gMyApp == nil) { return false; }
  52.     
  53.     if (inEvent->what == updateEvt)
  54.     {
  55.         gMyApp->DispatchEvent(*inEvent);
  56.     }
  57.  
  58.     return false;
  59. }
  60.  
  61. // ===========================================================================
  62. //        • Main Program
  63. // ===========================================================================
  64.  
  65. void main(void)
  66. {
  67.     OSStatus err;
  68.     
  69.     err = ISpGetGlobalElementList(&gElementList);
  70.     
  71.     err = ISpElementList_Extract(    gElementList,
  72.                                     kMaxGlobalElements, 
  73.                                     &gNumElements,
  74.                                     gElementBuffer);
  75.                             
  76.     gElement = gElementBuffer[gCurElement];
  77.     
  78.     err = ISpElementList_New(0, 0, &gElementList, 0);
  79.     
  80.     gStatus = err;
  81.         
  82.                                 // Set Debugging options
  83.     SetDebugThrow_(debugAction_Alert);
  84.     SetDebugSignal_(debugAction_Alert);
  85.  
  86.     InitializeHeap(3);                // Initialize Memory Manager
  87.                                     // Parameter is number of Master Pointer
  88.                                     //   blocks to allocate
  89.     
  90.                                     // Initialize standard Toolbox managers
  91.     UQDGlobals::InitializeToolbox(&qd);
  92.     
  93.     new LGrowZone(20000);            // Install a GrowZone function to catch
  94.                                     //    low memory situations.
  95.  
  96.     CPPStarterApp    theApp;            // replace this with your App type
  97.     gMyApp = &theApp;
  98.     theApp.Run();
  99.     
  100. }
  101.  
  102.  
  103. // ---------------------------------------------------------------------------
  104. //        • CPPStarterApp             // replace this with your App type
  105. // ---------------------------------------------------------------------------
  106. //    Constructor
  107.  
  108. CPPStarterApp::CPPStarterApp()
  109. {
  110.     URegistrar::RegisterClass(ISpElementView::class_ID, (ClassCreatorFunc) ISpElementView::CreateISpElementViewStream);
  111.     URegistrar::RegisterClass(ISpEventPane::class_ID, (ClassCreatorFunc) ISpEventPane::CreateISpEventPaneStream);
  112.     URegistrar::RegisterClass(LQuitWindow::class_ID, (ClassCreatorFunc) LQuitWindow::CreateLQuitWindowStream);
  113.     // Register functions to create core PowerPlant classes
  114.     
  115.     RegisterAllPPClasses();
  116. }
  117.  
  118.  
  119. // ---------------------------------------------------------------------------
  120. //        • ~CPPStarterApp            // replace this with your App type
  121. // ---------------------------------------------------------------------------
  122. //    Destructor
  123. //
  124.  
  125. CPPStarterApp::~CPPStarterApp()
  126. {
  127.     ISpElement_DisposeVirtual(kNumNeeds, mVirtualElements);
  128. }
  129.  
  130. // ---------------------------------------------------------------------------
  131. //        • StartUp
  132. // ---------------------------------------------------------------------------
  133. //    This function lets you do something when the application starts up. 
  134. //    For example, you could issue your own new command, or respond to a system
  135. //  oDoc (open document) event.
  136.  
  137. void
  138. CPPStarterApp::StartUp()
  139. {
  140.     InitNeeds();
  141.  
  142.     LWindow *theWindow;
  143.     
  144.     theWindow = LWindow::CreateWindow(1, this);    
  145.     theWindow->Show();
  146.  
  147.     theWindow = LWindow::CreateWindow(2, this);    
  148.     theWindow->Show();
  149.  
  150.  
  151. }
  152. static void EnableDeviceClass(OSType deviceClass, Boolean enable)
  153. {
  154.     enum {
  155.         kDeviceList_COUNT = 100
  156.     };
  157.     
  158.     OSStatus err;
  159.     UInt32 count;
  160.     ISpDeviceReference deviceList[kDeviceList_COUNT];
  161.     
  162.     // NOTE: This is not the correct way to handle the list count thing.  We
  163.     // should actually call once with NULL for the device list, malloc a list of
  164.     // that size, and call again.
  165.     
  166.     err = ISpDevices_ExtractByClass(
  167.             deviceClass,
  168.             kDeviceList_COUNT,
  169.             &count,
  170.             deviceList);
  171.     
  172.     if (err)
  173.     {
  174.         gStatus = err;
  175.         return;
  176.     }
  177.     
  178.     if (count > kDeviceList_COUNT)
  179.     {
  180.         count = kDeviceList_COUNT;
  181.     }
  182.     
  183.     if (enable)
  184.     {
  185.         err = ISpDevices_Activate(
  186.                 count,
  187.                 deviceList);
  188.         
  189.         if (err)
  190.         {
  191.             gStatus = err;
  192.             return;
  193.         }
  194.     }
  195.     else
  196.     {
  197.         err = ISpDevices_Deactivate(
  198.                 count,
  199.                 deviceList);
  200.         
  201.         if (err)
  202.         {
  203.             gStatus = err;
  204.             return;
  205.         }
  206.     }
  207.         
  208.     gStatus = err;
  209. }
  210.  
  211. // ---------------------------------------------------------------------------
  212. //        • ObeyCommand
  213. // ---------------------------------------------------------------------------
  214. //    Respond to commands
  215.  
  216. Boolean
  217. CPPStarterApp::ObeyCommand(
  218.     CommandT    inCommand,
  219.     void        *ioParam)
  220. {
  221.     Boolean        cmdHandled = true;
  222.  
  223.     switch (inCommand) {
  224.     
  225.         // Deal with command messages (defined in PP_Messages.h).
  226.         // Any that you don't handle will be passed to LApplication
  227.              
  228.         case cmd_New:
  229.                                         // EXAMPLE, create a new window
  230.             break;
  231.  
  232.  
  233.         case cmd_Suspend:
  234.             gStatus = ISpSuspend();
  235.             break;
  236.             
  237.         case cmd_Resume:
  238.             gStatus = ISpResume();
  239.             break;
  240.         
  241.         case cmd_NextElement:
  242.             gStatus = noErr;
  243.             gCurElement = (gCurElement + 1) % gNumElements;
  244.             gElement = gElementBuffer[gCurElement];
  245.             break;
  246.             
  247.         case cmd_PrevElement:
  248.             gStatus = noErr;
  249.             gCurElement = (gCurElement + gNumElements - 1) % gNumElements;
  250.             gElement = gElementBuffer[gCurElement];
  251.             break;
  252.  
  253.         case cmd_AddElement:
  254.             gStatus = ISpElementList_AddElements(    gElementList, 
  255.                                                     gCurElement,
  256.                                                     1,
  257.                                                     &gElement);
  258.             break;
  259.  
  260.         case cmd_DeleteElement:
  261.             gStatus = ISpElementList_RemoveElements(    gElementList, 
  262.                                                         1,
  263.                                                         &gElement);
  264.             break;
  265.  
  266.         case cmd_UIStart:
  267.             gStatus = ISpInit(    kNumNeeds,
  268.                                 mNeeds,
  269.                                 mVirtualElements,
  270.                                 kCreatorCode,
  271.                                 kSubCreatorCode,
  272.                                 0,    // flags
  273.                                 kResourceID_setl,
  274.                                 0); // reserved
  275.             break;
  276.             
  277.         case cmd_UIConfigure:
  278.             gStatus = ISpConfigure(&EventProc);
  279.             break;
  280.             
  281.         case cmd_UIStop:
  282.             gStatus = ISpStop();
  283.             break;
  284.     
  285.         case cmd_ActivateKeyboard:
  286.             EnableDeviceClass(kISpDeviceClass_Mouse, false);
  287.             EnableDeviceClass(kISpDeviceClass_Keyboard, true);
  288.             break;
  289.     
  290.         case cmd_ActiveMouse:
  291.             EnableDeviceClass(kISpDeviceClass_Keyboard, false);
  292.             EnableDeviceClass(kISpDeviceClass_Mouse, true);
  293.             break;
  294.             
  295.         case cmd_DeactiveBoth:
  296.             EnableDeviceClass(kISpDeviceClass_Mouse, false);
  297.             EnableDeviceClass(kISpDeviceClass_Keyboard, false);
  298.             break;
  299.  
  300.             
  301.         default:
  302.             cmdHandled = LApplication::ObeyCommand(inCommand, ioParam);
  303.             break;
  304.     }
  305.     
  306.     return cmdHandled;
  307. }
  308.  
  309. // ---------------------------------------------------------------------------
  310. //        • FindCommandStatus
  311. // ---------------------------------------------------------------------------
  312. //    This function enables menu commands.
  313. //
  314.  
  315. void
  316. CPPStarterApp::FindCommandStatus(
  317.     CommandT    inCommand,
  318.     Boolean        &outEnabled,
  319.     Boolean        &outUsesMark,
  320.     Char16        &outMark,
  321.     Str255        outName)
  322. {
  323.  
  324.     switch (inCommand) {
  325.     
  326.         // Return menu item status according to command messages.
  327.         // Any that you don't handle will be passed to LApplication
  328.  
  329.         case cmd_New:                    // EXAMPLE
  330.             outEnabled = false;
  331.             break;
  332.             
  333.         case cmd_Suspend:
  334.         case cmd_Resume:
  335.         case cmd_UIStart:
  336.         case cmd_UIConfigure:
  337.         case cmd_UIStop:
  338.             outEnabled = false;
  339.             break;
  340.             
  341.         case cmd_NextElement:
  342.         case cmd_PrevElement:
  343.         case cmd_AddElement:
  344.         case cmd_DeleteElement:
  345.         case cmd_ActivateKeyboard:
  346.         case cmd_ActiveMouse:
  347.         case cmd_DeactiveBoth:
  348.             outEnabled = true;            // enable the New command
  349.             break;
  350.  
  351.         default:
  352.             LApplication::FindCommandStatus(inCommand, outEnabled,
  353.                                                 outUsesMark, outMark, outName);
  354.             break;
  355.     }
  356. }
  357.  
  358. void CPPStarterApp::EventResume        (const EventRecord &inMacEvent)
  359. {
  360.     LApplication::EventResume(inMacEvent);
  361.  
  362.     ISpResume();
  363. }
  364.  
  365. void CPPStarterApp::EventSuspend    (const EventRecord &inMacEvent)
  366. {
  367.     ISpSuspend();
  368.     
  369.     LApplication::EventSuspend(inMacEvent);
  370. }
  371.  
  372. void CPPStarterApp::InitNeeds(void)
  373. {
  374.     ISpNeed tempNeeds[kNumNeeds] =
  375.     {
  376.         {
  377.             "\pForward Thrust",
  378.             kIconSuiteID_YThrust,
  379.             0,        // player number
  380.             0,        // group number
  381.             kISpElementKind_Axis,
  382.             kISpElementLabel_Axis_YAxis,
  383.             0,        // flags
  384.             0,        // reserved 1
  385.             0,        // reserved 2
  386.             0        // reserved 3
  387.         },
  388.         {
  389.             "\pSide Thrust",
  390.             kIconSuiteID_XThrust, 
  391.             0,        // player number
  392.             0,        // group number
  393.             kISpElementKind_Axis,
  394.             kISpElementLabel_Axis_XAxis,
  395.             0,        // flags
  396.             0,        // reserved 1
  397.             0,        // reserved 2
  398.             0        // reserved 3
  399.         },
  400.         {
  401.             "\pVertical Thrust",
  402.             kIconSuiteID_ZThrust, 
  403.             0,        // player number
  404.             0,        // group number
  405.             kISpElementKind_Axis,
  406.             kISpElementLabel_Axis_ZAxis,
  407.             0,        // flags
  408.             0,        // reserved 1
  409.             0,        // reserved 2
  410.             0        // reserved 3
  411.         },
  412.         {
  413.             "\pLook", 
  414.             kIconSuiteID_Look, 
  415.             0,        // player number
  416.             0,        // group number
  417.             kISpElementKind_Movement,
  418.             kISpElementLabel_None,
  419.             0,        // flags
  420.             0,        // reserved 1
  421.             0,        // reserved 2
  422.             0        // reserved 3
  423.         },
  424.         {
  425.             "\pFire", 
  426.             kIconSuiteID_Fire, 
  427.             0,        // player number
  428.             0,        // group number
  429.             kISpElementKind_Button,
  430.             kISpElementLabel_Btn_Fire,
  431.             0,        // flags
  432.             0,        // reserved 1
  433.             0,        // reserved 2
  434.             0        // reserved 3
  435.         },
  436.         {
  437.             "\pPause", 
  438.             kIconSuiteID_Pause, 
  439.             0,        // player number
  440.             0,        // group number
  441.             kISpElementKind_Button,
  442.             kISpElementLabel_None,
  443.             kISpNeedFlag_NoMultiConfig,    // flags
  444.             0,        // reserved 1
  445.             0,        // reserved 2
  446.             0        // reserved 3
  447.         },
  448.         {
  449.             "\pStart/Stop", 
  450.             kIconSuiteID_Start,
  451.             0,        // player number
  452.             0,        // group number
  453.             kISpElementKind_Button,
  454.             kISpElementLabel_Btn_PauseResume,
  455.             kISpNeedFlag_NoMultiConfig,        // flags
  456.             0,        // reserved 1
  457.             0,        // reserved 2
  458.             0        // reserved 3
  459.         },
  460.     };
  461.     
  462.  
  463.     int itr;
  464.     for(itr = 0; itr < kNumNeeds; itr++)
  465.     {
  466.         mNeeds[itr] = tempNeeds[itr];
  467.     }
  468.  
  469.     OSStatus err;
  470.       err = ISpElement_NewVirtualFromNeeds(kNumNeeds, mNeeds, mVirtualElements, 0);
  471.       
  472.       if (err != noErr) { DebugStr("\pISpElement_NewVirtualFromNeeds failed!"); }
  473.  
  474. }